package com.service.impl;

import com.dao.RoleDao;
import com.pojo.Role;
import com.service.RoleService;
import com.vo.RoleVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @author mornd
 * @date 2020/11/16 - 18:50
 */
@Service
@Transactional
public class RoleServiceImpl implements RoleService {
    @Autowired
    private RoleDao roleDao;
    public List<Role> findRoleList(RoleVo roleVo) {
        return roleDao.findRoleList(roleVo);
    }

    public int addRole(Role role) {
        return roleDao.addRole(role);
    }

    public int updateRole(Role role) {
        return roleDao.updateRole(role);
    }

    public int deleteRole(Integer roleId) {
        return roleDao.deleteRole(roleId);
    }

    public int checkRoleByUser(Integer roleId) {
        return roleDao.checkRoleByUser(roleId);
    }

    public List<Map<String, Object>> findRoleListByMap() {
        return roleDao.findRoleListByMap();
    }

    public List<Integer> findRoleIdListByUserId(Integer userId) {
        return roleDao.findRoleIdListByUserId(userId);
    }

    public boolean addRolePermission(String ids, Integer roleId) {
        //判断ids是否为null或者为空字符串
        boolean isEmpty = ids == null || "".equals(ids);
        try {
            //先清除角色菜单表的数据
            clearRolePermission(roleId);
            //再添加菜单
            if(!isEmpty){
                //将字符串分割为数组
                String[] splitByIds = ids.split(",");
                List<Integer> list = new ArrayList<Integer>();
                for (String splitById : splitByIds) {
                    list.add(Integer.valueOf(splitById));
                }
                roleDao.addRolePermission(list,roleId);
            }
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

    public int clearRolePermission(Integer roleId) {
        return roleDao.clearRolePermission(roleId);
    }
}
